home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 9C7VQH (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  9.7 KB  |  233 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.Icon;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JMenu;
  7. import com.sun.java.swing.JMenuBar;
  8. import com.sun.java.swing.JMenuItem;
  9. import com.sun.java.swing.JPopupMenu;
  10. import com.sun.java.swing.KeyStroke;
  11. import com.sun.java.swing.LookAndFeel;
  12. import com.sun.java.swing.MenuElement;
  13. import com.sun.java.swing.MenuSelectionManager;
  14. import com.sun.java.swing.Timer;
  15. import com.sun.java.swing.UIManager;
  16. import com.sun.java.swing.event.ChangeListener;
  17. import com.sun.java.swing.plaf.ComponentUI;
  18. import com.sun.java.swing.plaf.MenuUI;
  19. import com.sun.java.swing.plaf.UIResource;
  20. import java.awt.AWTEvent;
  21. import java.awt.Color;
  22. import java.awt.Component;
  23. import java.awt.Dimension;
  24. import java.awt.Graphics;
  25. import java.awt.Insets;
  26. import java.awt.Point;
  27. import java.awt.event.ComponentEvent;
  28. import java.awt.event.InputEvent;
  29. import java.awt.event.KeyEvent;
  30. import java.awt.event.MouseEvent;
  31. import java.awt.event.MouseMotionListener;
  32. import java.io.Serializable;
  33.  
  34. public class BasicMenuUI extends MenuUI implements Serializable {
  35.    protected JMenu menu;
  36.    protected MouseListener mouseListener;
  37.    protected MouseMotionListener dragListener;
  38.    protected ChangeListener menuChangeListener;
  39.    protected static Color pressedBackground;
  40.    protected static Color pressedForeground;
  41.    private int lastMnemonic = 0;
  42.    protected static final int defaultTextIconGap = 4;
  43.    protected Icon menuArrow = null;
  44.    protected Icon checkIcon = null;
  45.    protected boolean oldBorderPainted;
  46.  
  47.    protected void addListeners(JComponent c) {
  48.       ((Component)c).addMouseListener(this.mouseListener);
  49.       ((Component)c).addMouseMotionListener(this.dragListener);
  50.       ((JMenu)c).addChangeListener(this.menuChangeListener);
  51.    }
  52.  
  53.    protected ChangeListener createMenuChangeListener(JComponent c) {
  54.       return new MenuChangeListener((JMenu)c, this);
  55.    }
  56.  
  57.    protected MouseListener createMouseListener(JComponent c) {
  58.       return new MouseListener((JMenu)c);
  59.    }
  60.  
  61.    protected MouseMotionListener createMouseMotionListener(JComponent c) {
  62.       return new BasicMenuMouseMotionListener();
  63.    }
  64.  
  65.    public static ComponentUI createUI(JComponent x) {
  66.       return new BasicMenuUI();
  67.    }
  68.  
  69.    public Insets getDefaultMargin(AbstractButton c) {
  70.       return new Insets(2, 2, 2, 2);
  71.    }
  72.  
  73.    public Dimension getMaximumSize(JComponent c) {
  74.       return this.getPreferredSize(c);
  75.    }
  76.  
  77.    public Dimension getMinimumSize(JComponent c) {
  78.       return this.getPreferredSize(c);
  79.    }
  80.  
  81.    public Dimension getPreferredSize(JComponent c) {
  82.       this.installDefaultIcons();
  83.       return BasicGraphicsUtils.getPreferredMenuItemSize(c, this.checkIcon, this.menuArrow, 4);
  84.    }
  85.  
  86.    protected void initListeners(JComponent c) {
  87.       this.mouseListener = this.createMouseListener(c);
  88.       this.dragListener = this.createMouseMotionListener(c);
  89.       this.menuChangeListener = this.createMenuChangeListener(c);
  90.    }
  91.  
  92.    protected void installDefaultIcons() {
  93.       if (this.menu != null && this.menu.getParent() != null && !(this.menu.getParent() instanceof JMenuBar)) {
  94.          if (this.menuArrow == null || this.menuArrow instanceof UIResource) {
  95.             this.menuArrow = UIManager.getIcon("Menu.arrowIcon");
  96.          }
  97.  
  98.          if (this.checkIcon == null || this.checkIcon instanceof UIResource) {
  99.             this.checkIcon = UIManager.getIcon("MenuItem.checkIcon");
  100.          }
  101.       }
  102.  
  103.    }
  104.  
  105.    public void installUI(JComponent c) {
  106.       this.menu = (JMenu)c;
  107.       this.menu.setDelay(200);
  108.       this.initListeners(c);
  109.       this.addListeners(c);
  110.       c.setOpaque(true);
  111.       LookAndFeel.installBorder(c, "Menu.border");
  112.       this.oldBorderPainted = this.menu.isBorderPainted();
  113.       this.menu.setBorderPainted((Boolean)UIManager.get("MenuItem.borderPainted"));
  114.       LookAndFeel.installColorsAndFont(c, "Menu.background", "Menu.foreground", "Menu.font");
  115.       this.validateKeyboardAccelerator();
  116.       if (pressedBackground == null || pressedBackground instanceof UIResource) {
  117.          pressedBackground = UIManager.getColor("Menu.pressedBackground");
  118.       }
  119.  
  120.       if (pressedForeground == null || pressedForeground instanceof UIResource) {
  121.          pressedForeground = UIManager.getColor("Menu.pressedForeground");
  122.       }
  123.  
  124.    }
  125.  
  126.    private int lower(int ascii) {
  127.       return ascii >= 65 && ascii <= 90 ? ascii + 97 - 65 : ascii;
  128.    }
  129.  
  130.    public void menuCanceled(JMenu m) {
  131.       MenuSelectionManager manager = MenuSelectionManager.defaultManager();
  132.       if (manager.isComponentPartOfCurrentMenu(m)) {
  133.          MenuSelectionManager.defaultManager().clearSelectedPath();
  134.       }
  135.  
  136.    }
  137.  
  138.    public void paint(Graphics g, JComponent c) {
  139.       this.installDefaultIcons();
  140.       ((AbstractButton)c).getModel();
  141.       BasicGraphicsUtils.paintMenuItem(g, c, this.checkIcon, this.menuArrow, pressedBackground, pressedForeground, 4);
  142.    }
  143.  
  144.    public void processKeyEvent(JMenuItem item, KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
  145.       int key = ((AbstractButton)item).getMnemonic();
  146.       if (key != 0) {
  147.          if (((AWTEvent)e).getID() == 401 && this.lower(key) == this.lower(e.getKeyChar())) {
  148.             JPopupMenu popupMenu = ((JMenu)item).getPopupMenu();
  149.             MenuElement[] sub = popupMenu.getSubElements();
  150.             if (sub.length > 0) {
  151.                MenuElement[] newPath = new MenuElement[path.length + 2];
  152.                System.arraycopy(path, 0, newPath, 0, path.length);
  153.                newPath[path.length] = popupMenu;
  154.                newPath[path.length + 1] = sub[0];
  155.                manager.setSelectedPath(newPath);
  156.             }
  157.  
  158.             ((InputEvent)e).consume();
  159.          }
  160.  
  161.       }
  162.    }
  163.  
  164.    public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement[] path, MenuSelectionManager manager) {
  165.       Point p = e.getPoint();
  166.       if (((Component)item).isEnabled()) {
  167.          if (p.x >= 0 && p.x < ((JComponent)item).getWidth() && p.y >= 0 && p.y < ((JComponent)item).getHeight()) {
  168.             JMenu menu = (JMenu)item;
  169.             MenuElement[] selectedPath = manager.getSelectedPath();
  170.             if (selectedPath.length <= 0 || selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) {
  171.                if (!menu.isTopLevelMenu() && menu.getDelay() != 0 && ((AWTEvent)e).getID() != 506) {
  172.                   manager.setSelectedPath(path);
  173.                   this.setupPostTimer(menu);
  174.                } else {
  175.                   MenuElement[] newPath = new MenuElement[path.length + 1];
  176.                   System.arraycopy(path, 0, newPath, 0, path.length);
  177.                   newPath[path.length] = menu.getPopupMenu();
  178.                   manager.setSelectedPath(newPath);
  179.                }
  180.             }
  181.          } else if (((AWTEvent)e).getID() == 502) {
  182.             Component c = manager.componentForPoint(((ComponentEvent)e).getComponent(), e.getPoint());
  183.             if (c == null) {
  184.                manager.clearSelectedPath();
  185.             }
  186.          }
  187.  
  188.       }
  189.    }
  190.  
  191.    protected void removeListeners(JComponent c) {
  192.       ((Component)c).removeMouseListener(this.mouseListener);
  193.       ((Component)c).removeMouseMotionListener(this.dragListener);
  194.       ((JMenu)c).removeChangeListener(this.menuChangeListener);
  195.    }
  196.  
  197.    protected void setupPostTimer(JMenu menu) {
  198.       Timer timer = new Timer(menu.getDelay(), new PostAction(menu, false));
  199.       timer.setRepeats(false);
  200.       timer.start();
  201.    }
  202.  
  203.    public void uninstallUI(JComponent c) {
  204.       this.menu.setArmed(false);
  205.       this.menu.setSelected(false);
  206.       this.removeListeners(c);
  207.       c.resetKeyboardActions();
  208.       LookAndFeel.uninstallBorder(c);
  209.       ((JMenu)c).setBorderPainted(this.oldBorderPainted);
  210.       if (this.menuArrow instanceof UIResource) {
  211.          this.menuArrow = null;
  212.       }
  213.  
  214.       if (this.checkIcon instanceof UIResource) {
  215.          this.checkIcon = null;
  216.       }
  217.  
  218.    }
  219.  
  220.    public void update(Graphics g, JComponent c) {
  221.       this.paint(g, c);
  222.    }
  223.  
  224.    protected void validateKeyboardAccelerator() {
  225.       if (this.menu.getModel().getMnemonic() != this.lastMnemonic) {
  226.          this.menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(this.lastMnemonic, 8, false));
  227.          this.lastMnemonic = this.menu.getModel().getMnemonic();
  228.          this.menu.registerKeyboardAction(new PostAction(this.menu, true), KeyStroke.getKeyStroke(this.lastMnemonic, 8, false), 2);
  229.       }
  230.  
  231.    }
  232. }
  233.